package com.chixing.ih.module.programmer.service.Impl;

import com.chixing.ih.module.programmer.mapper.*;
import com.chixing.ih.module.programmer.pojo.Attention;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.UserImage;
import com.chixing.ih.module.programmer.pojo.Work;
import com.chixing.ih.module.programmer.pojo.dto.MyHomePageDTO;
import com.chixing.ih.module.programmer.pojo.dto.PGHomePageDTO;
import com.chixing.ih.module.programmer.service.IHomePageService;
import com.chixing.ih.util.PageViewTotalUtil;
import com.chixing.ih.util.RedisPraiseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.jar.JarOutputStream;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/5
 * Time: 21:21
 */
@Service
@Transactional
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    ProgrammerMapper programmerMapper;

    @Autowired
    WorkMapper workMapper;

    @Autowired
    ImageMapper imageMapper;

    @Autowired
    AttentionMapper attentionMapper;


    @Autowired
    EvaluateMapper evaluateMapper;


    @Override
    public MyHomePageDTO findMyHomePage(HttpServletRequest request, long userId) {
        MyHomePageDTO myHomePageDTO = new MyHomePageDTO();
        Programmer programmer = programmerMapper.getOne(userId);
        if (programmer != null)
            myHomePageDTO.setProgrammer(programmer);
        Work work = workMapper.getOne(userId);
        if (work != null)
            myHomePageDTO.setWork(work);
        UserImage userImage = imageMapper.getOneByUserId(userId);
        if (userImage != null)
            myHomePageDTO.setImageUrl(userImage.getImageUrl());
        myHomePageDTO.setFabulousNum(RedisPraiseUtil.redisGetTotal(userId));
        Long views = PageViewTotalUtil.GetRedisCache(userId);
        if (views != null)
            myHomePageDTO.setPageViews(views);
        Long fanNum = attentionMapper.fanNum(userId);
        if (fanNum != null)
            myHomePageDTO.setFanNum(fanNum);
        Long followers = attentionMapper.followerNum(userId);
        if (followers != null)
            myHomePageDTO.setFollowerNum(followers);
        Double starRating = evaluateMapper.getStarRating(userId);
        if (starRating != null)
            myHomePageDTO.setStarRating(starRating);
        return myHomePageDTO;
    }

    @Override
    public PGHomePageDTO findPGHomePage(HttpServletRequest request, long visitorId, long visitedId) {
        PageViewTotalUtil.SetRedisCache(visitedId);
        PGHomePageDTO pgHomePageDTO = new PGHomePageDTO();
        Programmer programmer = programmerMapper.getOne(visitedId);
        if (programmer != null)
            pgHomePageDTO.setProgrammer(programmer);
        Work work = workMapper.getOne(visitedId);
        if (work != null)
            pgHomePageDTO.setWork(work);
        UserImage userImage = imageMapper.getOneByUserId(visitedId);
        if (userImage != null)
            pgHomePageDTO.setImageUrl(userImage.getImageUrl());
        pgHomePageDTO.setFabulousNum(RedisPraiseUtil.redisGetTotal(visitedId));
        Long views = PageViewTotalUtil.GetRedisCache(visitedId);
        if (views != null)
            pgHomePageDTO.setPageViews(views);
        Long fanNum = attentionMapper.fanNum(visitedId);
        if (fanNum != null)
            pgHomePageDTO.setFanNum(fanNum);
        Long followers = attentionMapper.followerNum(visitedId);
        if (followers != null)
            pgHomePageDTO.setFollowerNum(followers);
        Attention attention = new Attention();
        attention.setFollowerId(visitorId);
        attention.setFollowedId(visitedId);
        Integer followerStatus = attentionMapper.getStatus(attention);
        if (followerStatus != null)
            pgHomePageDTO.setFollowStatus(followerStatus);
        else
            pgHomePageDTO.setFollowStatus(1);
        Integer fabulousStatus = RedisPraiseUtil.redisGetState(visitorId, visitedId);
        if (fabulousStatus != null)
            pgHomePageDTO.setFabulousStatus(fabulousStatus);
        if (followers != null)
            pgHomePageDTO.setFollowerNum(followers);
        Double starRating = evaluateMapper.getStarRating(visitedId);
        if (starRating != null)
            pgHomePageDTO.setStarRating(starRating);
        else
            pgHomePageDTO.setStarRating(0.0);
        return pgHomePageDTO;
    }
}
